/*
 * Copyright (c) 2017. danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.dlware.net;

import android.content.Context;

import cn.com.dhc.danlu.util.CommonConst;
import cn.com.hd.mall.web.webservices.entity.BaseResponse;

/**
 * author: wuhaiyang(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-02-08 上午10:40<br/>
 * <p>
 * 处理通用业务情况订阅者，适用于丹露业务
 * </p>
 */
public abstract class GeneralBusinessObserver <T extends BaseResponse> extends ProgressObserver<T> {
    private String successCode = CommonConst.WebservicesConst.LOGIN_RESPONSE_SUCCESS_0;

    //不关心加载 对话框交互，只关心业务状态码是否正确
    public GeneralBusinessObserver() {
        super();
    }

    public GeneralBusinessObserver(String successCode) {
        super();
        this.successCode = successCode;
    }

    // 默认业务状态码，关心加载对话框交互
    public GeneralBusinessObserver(Context context, boolean isShowLoaddingDialg, int loaddingMsgId, boolean cancelable) {
        super(context, isShowLoaddingDialg, loaddingMsgId, cancelable);
    }

    public GeneralBusinessObserver(Context context, int loaddingMsgId) {
        super(context, loaddingMsgId);
    }

    // 关心业务状态码和加载交互
    public GeneralBusinessObserver(Context context, boolean isShowLoaddingDialg, int loaddingMsgId, boolean cancelable, String successCode) {
        super(context, isShowLoaddingDialg, loaddingMsgId, cancelable);
        this.successCode = successCode;
    }

    public GeneralBusinessObserver(Context context, int loaddingMsgId, String successCode) {
        super(context, loaddingMsgId);
        this.successCode = successCode;
    }

    @Override
    public void onNext(T t) {
        // bug
        String success = t.getSuccess();
        if (success.equals(successCode)) {
            onSuccess(t);
        } else {
            String msg = parseBussinessException(success);
            onAlertTips(msg);
        }
    }

    /**
     * if child class need show diff tips, you should rewrite this method by code
     */
    protected String parseBussinessException(String code) {
        return "";
    }

    protected abstract void onSuccess(T t);
}
